package java_thinking.unit_13._13_6;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description :
 * @author: cww
 * @DateTime: 2019-03-25 21:38
 */

public class TestRegularExpression {
    public static void main(String[] args) {
        String[] newArgs = {"abcaacabcdefabc","dfd","fefefe"};
        if (newArgs.length<2){
            System.out.println("Usage:\njava TestRegularExpression characterSequence " +
                    "regularExpression");
            System.exit(0);
        }
        System.out.println("Input: \""+newArgs[0]+"\"");
        for (String arg : newArgs){
            System.out.println("Regular expression: \""+arg+"\"");
            arg = "[a]+";
            Pattern p = Pattern.compile(arg);
            Matcher m = p.matcher(newArgs[0]);
            while (m.find()){
                System.out.println("Match \""+m.group()+"\" at positions "+m.start()+"-"+(m.end()-1));
            }
        }
    }
}

class TestRegularExpression1 {
    public static void main(String[] args) {
        String source = "Java now has regular expressions";
        String[] regEx = {"^Java", "\\Breg.*",
                "n.w\\s+h(a|i)s", "s?", "s*", "s+", "s{4}", "s{1}.",
                "s{0,3}"};
        System.out.println("Source string: " + source);
        for(String pattern : regEx) {
            System.out.println("Regular expression: \"" + pattern + "\"");
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(source);
            while(m.find()) {
                System.out.println("Match \"" + m.group() +
                        "\" at positions " + m.start() + "-" +
                        (m.end() - 1));
            }
        }
    }
}
